import {
  uploadProfile,
  getUserInfo,
  updateMail,
  updateAppointedUser,
  getEstimation,
  getToEstimation,
  estimate
} from '@/api/user'
import { useUserStore } from '@/stores'
import type { BasicInfo, StudentInfo, TeacherInfo } from '@/models/userinfo'
import { calculateAge } from '@/utils/dateUtil'

export async function UpdateMail(newMail: string, code: string): Promise<void> {
  await updateMail(newMail, code)
}

export async function UploadProfile(profile: File): Promise<string> {
  const res = (await uploadProfile(profile)) as any
  return res.data
}

export async function GetUserInfo(): Promise<any> {
  const res = (await getUserInfo()) as any
  const data = res.data
  const basicInfo: BasicInfo = {
    name: data.name,
    gender: data.gender,
    age: calculateAge(data.birthday),
    birthday: data.birthday,
    politics: data.politics,
    phone: data.phone,
    nationality: data.nationality,
    signature: data.signature,
    introduction: data.introduction,
    address: data.address,
    origin: data.origin,
    scienceResearch: data.scienceResearch
  }
  let info: any
  switch (useUserStore().role) {
    case 'STUDENT': {
      info = {
        basicInfo: basicInfo,
        academy: data.academy,
        major: data.major,
        degree: data.degree,
        classNumber: data.classNumber,
        length: data.length,
        number: data.number,
        specialty: data.specialty
      } as StudentInfo
      break
    }
    case 'TEACHER': {
      info = {
        basicInfo: basicInfo,
        academy: data.academy,
        researchDirection: data.researchDirection
      } as TeacherInfo
    }
  }
  return info
}

export async function UpdateAppointedUser(
  id: number,
  x: string,
  y: string,
  z: string,
  flag: boolean
): Promise<void> {
  await updateAppointedUser(id, x, y, z, flag)
}

export async function GetEstimation(): Promise<{
  practice: number
  moral: number
  attitude: number
  total: number
}> {
  const res = (await getEstimation()) as any
  return res.data
}
export async function GetToEstimation(): Promise<{ id: number; name: string }[]> {
  const res = (await getToEstimation()) as any
  const data = res.data as any[]
  const result: { id: number; name: string }[] = []
  data.forEach((val) => {
    if (val.moral === -1 || val.attitude === -1 || val.practice === -1) {
      result.push({ name: val.nameOfEvaluated, id: val.id })
    }
  })
  return result
}
export async function Estimate(id: number, moral: number, attitude: number, practice: number) {
  await estimate(id, moral, attitude, practice)
}
